<?
/*
 * Copyright 2004-2005 Sigve Indregard.
 *
 * This file is part of Laivsys.
 *
 * Laivsys is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Laivsys is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Laivsys; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * $Id: roller.php 6 2005-07-11 23:14:47Z say $
 */
 
include("../db.php");
include("auth.php");
include("top.php");
include("rollerinclude.php");
rollermeny();

if ($_REQUEST['process'] == 'slett2') {
	$sql="DELETE FROM r_Rolle WHERE ID=".$_REQUEST['rid'];
	mysql_query($sql);
} elseif ($_REQUEST['process'] == 'slett') {
	$rolle=getRolle($_REQUEST['rid']);
	?>
	<h2>Sletting</h2>
	Sikker p� at du vil slette <?=$rolle['rollekode']?>? Rollebeskrivelsen
	er p� <?=strlen($rolle['beskrivelse'])?> tegn.<p>
	<a href="roller.php?process=slett2&rid=<?=$rolle['ID']?>">[Ja]</a> | 
	<a href="roller.php">[Nei]</a>
	<?
}

if (isset($_REQUEST['vistilhor'])) {
	$vistilhor = $_REQUEST['vistilhor'];
	$_SESSION['vistilhor']=$vistilhor;
} elseif (isset($_SESSION['vistilhor'])) {
	$vistilhor = $_SESSION['vistilhor'];
} else {
	$vistilhor = "nei";
	$_SESSION['vistilhor'] = $vistilhor;
}

?>
<h2>Rolleoversikt</h2>
<table class="gridded">
	<tr>
		<?
		if (isset($_REQUEST['sort'])) {
			$sort=$_REQUEST['sort'];
			$_SESSION['sortrolle'] = $sort;
		} elseif (isset($_SESSION['sortrolle'])) {
			$sort=$_SESSION['sortrolle'];
		} else {
			$sort = 'rollekode';
			$_SESSION['sortrolle'] = $sort;
		}			
		
		$arr=array("Kode"=>"rollekode", "Navn"=>"rollenavn", "Etternavn"=>"familienavn", "K"=>"kjonn", "Status"=>"status", "Besk."=>"sizeofbeskrivelse", "Tilord."=>"eierID");
		
		if (! in_array($sort, $arr) ) {
			$sort='rollekode';
			$_SESSION['sortrolle'] = $sort;
		}
		
		foreach ($arr as $navn=>$felt) {
			if ($sort == $felt)
				echo "<th>$navn</th>\n";
			else
				echo "<th><a href=\"roller.php?sort=$felt\">$navn</a></th>";
		}
		?>
		<th></th>
		<th></th>
	</tr>
<?
$sql="SELECT ID, beskrivelse, rollekode, rollenavn, familienavn, kjonn, status, eierID, CHAR_LENGTH(beskrivelse) as sizeofbeskrivelse FROM r_Rolle WHERE LaivID=$laivid ORDER BY $sort, rollekode";
$roller=mysql_query($sql);
while ($rolle=mysql_fetch_assoc($roller)) {
	$rid = $rolle['ID'];
	?>
	<tr>
		<td><?=$rolle['rollekode']?></td>
		<td><?=$rolle['rollenavn']?></td>
		<td><?=$rolle['familienavn']?></td>
		<td><?=$rolle['kjonn']?></td>
		<td><?=getStatus($rolle['status'])?></td>
		<? 
		if (is_null($rolle['sizeofbeskrivelse']))
			$text = "0 tegn";
		else
			$text = $rolle['sizeofbeskrivelse']." tegn";
		?>
		<td align="right"><?=$text?></td>
		<td><?
		if (! is_null($rolle['eierID']) && $rolle['eierID'] != 0)
			echo "Ja";
		else
			echo "Nei";		
		?></td>
		<td><a href="rolledetaljer.php?id=<?=$rolle['ID']?>">[Mer]</a></td>
		<td><a href="roller.php?process=slett&rid=<?=$rolle['ID']?>">[Slett]</a></td>
	</tr>
	<?
	if ($vistilhor == "ja") {
		$sql="SELECT navn FROM r_Gruppemedlem LEFT JOIN r_Gruppe ON r_Gruppemedlem.gruppeID=r_Gruppe.ID WHERE rolleID=$rid ORDER BY navn";
		$grupper=mysql_query($sql);
		if (mysql_num_rows($grupper)) {
			?>
			<tr>
				<td colspan="9"><ul>
				<?
				while ($gruppe = mysql_fetch_row($grupper))
					echo "<li>{$gruppe[0]}</li>\n";
				?>
				</ul></td>
			</tr>
			<?
		}
	}
}
?>
</table>

<h2>Statistikk</h2>
<table style="width: 200px" class="gridded">
<?

function getNum($felt, $conditional, $compare='=') {
	global $laivid;
	$res=mysql_query("SELECT COUNT($felt) FROM r_Rolle WHERE $felt$compare$conditional AND LaivID=$laivid");
	if (! mysql_num_rows($res))
		return 0;
	else {
		$resset=mysql_fetch_row($res);
		return $resset[0];
	}	
}

function printNum($var, $navn) {
	echo "<tr>\n\t<td>$navn</td>\n\t<td style=\"text-align: right\">$var</td>\n</tr>";
}

printNum(getNum("ID", "0", ">"), 'Totalt');
printNum(getNum("kjonn", "'m'"), 'Menn');
printNum(getNum("kjonn", "'k'"), 'Kvinner');
printNum(getNum("status", "'F'"), 'Ferdige');
printNum(getNum("status", "'F'", "<>"), 'Uferdige');

?>
</table>
<?
include("bottom.php");
?>
